

package com.example.administrator.caigou51.recyclerCard.cardView;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.ab.util.AbViewUtil;
import com.example.administrator.caigou51.R;
import com.example.administrator.caigou51.activity.GBaseActivity;
import com.example.administrator.caigou51.recyclerCard.basic.CardItemView;
import com.example.administrator.caigou51.recyclerCard.basic.MaterialListView;
import com.example.administrator.caigou51.recyclerCard.basic.OnButtonPressListener;
import com.example.administrator.caigou51.recyclerCard.card.UseSayDetailCard;
import com.example.administrator.caigou51.recyclerCard.card.ZhuangQu1ChildCard;
import com.example.administrator.caigou51.recyclerCard.card.ZhuangQuCard1;
import com.example.administrator.caigou51.recyclerCard.card.ZhuangQuCard2;
import com.example.administrator.caigou51.recyclerCard.card.basic.Card;


public class ZhuangQuCard1View extends CardItemView<ZhuangQuCard1> {
 	private Context mContext;
    private MaterialListView materialListView;
    private ImageView imageViewMore;
    private TextView textViewSYFL;
    private ZhuangQuCard1 zhuangQuCard1;
    public ZhuangQuCard1View(Context context) {
        super(context);
        mContext=context;
        // TODO Auto-generated constructor stub
    }

    public ZhuangQuCard1View(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext=context;
        // TODO Auto-generated constructor stub
    }

    public ZhuangQuCard1View(Context context,
                             AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext=context;
        // TODO Auto-generated constructor stub
    }



    @Override
    public void build(ZhuangQuCard1 card) {
        // TODO Auto-generated method stub
        super.build(card);
        zhuangQuCard1=card;
        materialListView= (MaterialListView) findViewById(R.id.materialListView);materialListView.setVisibility(View.VISIBLE);
        imageViewMore= (ImageView) findViewById(R.id.imageViewMore);
        textViewSYFL= (TextView) findViewById(R.id.textViewSYFL);textViewSYFL.setVisibility(View.GONE);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
        materialListView.setLayoutManager(linearLayoutManager);
        materialListView.getLayoutParams().height= AbViewUtil.scaleValue(mContext,84);
        materialListView.setTag(AbViewUtil.NotScale);
        materialListView.clear();
        for (int i=0;i<card.getZhuangQuXiangQingBean().getSub_cat().size();i++) {
            ZhuangQu1ChildCard zhuangQu1ChildCard = new ZhuangQu1ChildCard(mContext);
            zhuangQu1ChildCard.setPosition(i);
            zhuangQu1ChildCard.setSubCatEntity(card.getZhuangQuXiangQingBean().getSub_cat().get(i));
            if (i==card.getIndex()) zhuangQu1ChildCard.setIschecked(true);
            zhuangQu1ChildCard.setMaterialListView(materialListView);
            materialListView.add(zhuangQu1ChildCard);
        }
        materialListView.scrollToPosition(card.getIndex());
        imageViewMore.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopupWindow(imageViewMore);
                imageViewMore.setImageResource(R.drawable.icon_shangbiao);
                textViewSYFL.setVisibility(View.VISIBLE);
                materialListView.setVisibility(View.GONE);
            }
        });
    }


    private void showPopupWindow(View view) {

        // 一个自定义的布局，作为显示的内容
        View contentView = LayoutInflater.from(mContext).inflate(
                R.layout.layout_more_fenlei, null);

        AbViewUtil.scaleContentView((ViewGroup) contentView);

        final PopupWindow popupWindow = new PopupWindow(contentView,
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true);

        //  AbViewUtil.scaleContentView((ViewGroup) popupWindow.getContentView());

        MaterialListView listView= (MaterialListView) contentView.findViewById(R.id.material_listview);
        listView.setColumnLayout(3);

        for (int i=0;i<zhuangQuCard1.getZhuangQuXiangQingBean().getSub_cat().size();i++)
        {
            UseSayDetailCard useSayDetailCard=new UseSayDetailCard(mContext);
            useSayDetailCard.setSubCatEntity(zhuangQuCard1.getZhuangQuXiangQingBean().getSub_cat().get(i));
            useSayDetailCard.setPopupWindow(popupWindow);
            useSayDetailCard.setImageViewMore(imageViewMore);
            useSayDetailCard.setTextViewSYFL(textViewSYFL);
            useSayDetailCard.setMaterialListView(materialListView);
            useSayDetailCard.setIndex(i);
            listView.add(useSayDetailCard);
        /*    useSayDetailCard.setOnNormalButtonPressedListener(new OnButtonPressListener() {
                @Override
                public void onButtonPressedListener(View view, Card card) {
                    Log.e("materialListView", "onTouch : ");

                    popupWindow.dismiss();
                    imageViewMore.setImageResource(R.drawable.icon_xiabiao);
                    textViewSYFL.setVisibility(View.GONE);
                    materialListView.setVisibility(View.VISIBLE);
                }
            });*/
        }

/*        View viewBg=contentView.findViewById(R.id.viewBg);
        viewBg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });*/
     //   popupWindow.setTouchable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);

      /*  popupWindow.setTouchInterceptor(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                Log.e("mengdd", "onTouch : ");
                popupWindow.dismiss();
                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
            }
        });*/

        // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
        // 我觉得这里是API的一个bug
        //  popupWindow.setBackgroundDrawable(getResources().getDrawable(new BitmapDrawable());
        popupWindow.setBackgroundDrawable(new BitmapDrawable());

        // 设置好参数之后再show
        popupWindow.showAsDropDown(view,0,AbViewUtil.scaleValue(mContext,4));
        // popupWindow.showAtLocation(view, Gravity.RIGHT|Gravity.BOTTOM, 0, 0);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                popupWindow.dismiss();
                imageViewMore.setImageResource(R.drawable.icon_xiabiao);
                textViewSYFL.setVisibility(View.GONE);
                materialListView.setVisibility(View.VISIBLE);
            }
        });
    }
}
